package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.QAdoptedTreeOrder;
import com.xinhe.web.entity.QMember;
import com.xinhe.web.entity.QStudio;
import com.xinhe.web.entity.Studio;
import com.xinhe.web.reposity.StudioRepository;
import com.xinhe.web.response.AdoptedTreeOrderView;
import com.xinhe.web.response.StudioView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class StudioService {
    @Autowired
    private StudioRepository studioRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }


    public QueryResults<StudioView> pageStudio(Long userId, String name, String codeName, String address, String longitude,
                                               String latitude, Integer seq, String remark,int pageNum, int pageSize) {
        QStudio qStudio = QStudio.studio;
        QMember qMember = QMember.member;
        BooleanBuilder builder = new BooleanBuilder();
        if (!Objects.isNull(userId)){
            builder.and(qStudio.userId.eq(userId));
        }
        if (!Objects.isNull(name)){
            builder.and(qStudio.name.eq(name));
        }
        if (StringUtils.isNotBlank(codeName)){
            builder.and(qStudio.codeName.eq(codeName));
        }
        if (!Objects.isNull(address)){
            builder.and(qStudio.address.eq(address));
        }
        if (!Objects.isNull(latitude)){
            builder.and(qStudio.latitude.eq(latitude));
        }
        if (!Objects.isNull(longitude)){
            builder.and(qStudio.longitude.eq(longitude));
        }
        if (!Objects.isNull(seq)){
            builder.and(qStudio.seq.eq(seq));
        }
        return queryFactory.select(
                        Projections.bean(
                                StudioView.class,
                                qStudio.id,
                                qStudio.address,
                                qStudio.userId,
                                qStudio.longitude,
                                qStudio.latitude,
                                qStudio.codeName,
                                qStudio.seq,
                                qMember.nickname.as("userName")
                        )
                ).from(qStudio)
                .leftJoin(qMember).on(qStudio.userId.eq(qMember.fUserId))
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    @Transactional
    public Result saveStudio(Long id,Long userId, String name, String codeName, String address, String longitude,
                                 String latitude, Integer seq, String remark) {
        Studio studio = null;
        if (null != id) {
            studio = this.studioRepository.findById(id).get();
            studio.setAddress(address);
            studio.setCodeName(codeName);
            studio.setLatitude(latitude);
            studio.setLongitude(longitude);
            studio.setUserId(userId);
            studio.setName(name);
            studio.setSeq(seq);
            this.studioRepository.saveAndFlush(studio);
        }else{
            Integer count = this.studioRepository.findByUserId(userId);
            if (count.intValue() > 0) {
                Result.failed(1001,"该用户已设定工作室,请重新选择");
            }
            studio = new Studio();
            studio.setAddress(address);
            studio.setCodeName(codeName);
            studio.setLatitude(latitude);
            studio.setLongitude(longitude);
            studio.setUserId(userId);
            studio.setName(name);
            studio.setSeq(seq);
            this.studioRepository.save(studio);
        }
        return Result.ok();
    }


    public Result<Studio> findById(Long studioId) {
        return Result.ok(studioRepository.findById(studioId).get());
    }

    public Result delete(Long studioId) {
        studioRepository.deleteById(studioId);
        return Result.ok();
    }
}
